import request from "@/utils/request";
import { LoginData, LoginResult, VerifyCodeResult, TenantInfo } from "./types";
import { AxiosPromise } from "axios";
import { UserInfo } from "@/api/system/user/types";

// pc端固定客户端授权id
const clientId = import.meta.env.VITE_APP_CLIENT_ID;
/**
 * @name 登录模块
 */
// 用户登录
export const loginApi = (data: LoginData): AxiosPromise<LoginResult> => {
  const params = {
    ...data,
    clientId: data.clientId || clientId,
    grantType: data.grantType || "password"
  };
  return request({
    url: "/auth/login",
    headers: {
      isToken: false,
      isEncrypt: true
    },
    method: "post",
    data: params
  });
};

export const getUserInfoApi = (): AxiosPromise<UserInfo> => {
  return request({
    url: "/system/user/getInfo",
    method: "get"
  });
};

// 获取路由
export function getRouters(): AxiosPromise<MenuOptions[]> {
  return request({
    url: "/system/menu/getRouters",
    method: "get"
  });
}

/**
 * 注销
 */
export function logoutApi() {
  return request({
    url: "/auth/logout",
    method: "post"
  });
}

/**
 * 获取验证码
 */
export function getCodeImg(): AxiosPromise<VerifyCodeResult> {
  return request({
    url: "/auth/code",
    headers: {
      isToken: false
    },
    method: "get",
    timeout: 20000
  });
}

// 获取租户列表
export function getTenantList(): AxiosPromise<TenantInfo> {
  return request({
    url: "/auth/tenant/list",
    headers: {
      isToken: false
    },
    method: "get"
  });
}
